package com.yanqu.road.dataHelper;

import com.yanqu.road.dataHelper.action.userCenterScript.ActivityScript;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.*;
import com.yanqu.road.dataHelper.repair.mgr.RepairMainTaskMgr;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.logic.redis.RedisManger;
import com.yanqu.road.utils.ConfigHelper;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class DbScript {

    // 修复的类名
    static String repairClassName;
    // 服务器列表
    static String whereStr;
    static String serverStr;
    static List<Long> serverList;
    List<BaseMgr> mgrList = new ArrayList<>();

    public static final String CONFIG_DIR = System.getProperty("user.dir") + File.separator + "Config" + File.separator;

    //参数1调用的方法,参数2为区服
    public static void main(String[] args){
        try {
            repairClassName = args[0];
            whereStr = args[1];
//            serverStr = args[1];
//            serverList = StringUtils.stringToLongList(serverStr,",");
        }catch (Exception e){
            e.printStackTrace();
            return;
        }
        String dirPath = System.getProperty("user.dir") + File.separator;
        DbScript dbScript = new DbScript();
        if(dbScript.run(dirPath)){
            System.err.println("execute over");
            System.exit(1);
        }else{
            System.err.println("game over");
            System.exit(0);
        }
    }

    private boolean run(String dirPath){
        if (!DbMgr.initDB(dirPath)) {
            return false;
        }
        if (!GameMgr.init(dirPath)) {
            return false;
        }
        String redisFile = dirPath + "config" + File.separator + "redis.properties";
        RedisManger.initConfig(redisFile);

        initMgr();

        serverList = getMgr(ServerMgr.class).getWhereServer(whereStr);
        try {
            Class actionClass = Class.forName("com.yanqu.road.dataHelper.action." + repairClassName.trim());
            Method method = actionClass.getMethod("go",new Class[]{List.class});
            Constructor c = actionClass.getConstructor();
            Object actionObject = c.newInstance();
            method.invoke(actionObject, new Object[]{serverList});
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

        DataMgr.outExcel(repairClassName);
        DataMgr.outText(repairClassName);

        return true;
    }

    private boolean runOld(String dirPath){
        if (!DbMgr.initDB(dirPath)) {
            return false;
        }
        if (!GameMgr.init(dirPath)) {
            return false;
        }

        initMgr();

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        List<Future> futureList = new ArrayList<>();
        if("0".equals(whereStr)){
            //不是去区服统计
        }else{
            serverList = getMgr(ServerMgr.class).getWhereServer(whereStr);
            for(Long serverId : serverList){
                if(ServerMgr.getServerInfoList().containsKey(serverId)){
                    ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                    Future future = executorService.submit(new Callable<Boolean>() {
                        @Override
                        public Boolean call() throws Exception {
                            GameServer testGameServer = new GameServer(serverInfo);
                            try {
                                Class repairClass = Class.forName("com.yanqu.road.dataHelper.repair.logic." + repairClassName.trim());
                                Constructor c = repairClass.getConstructor(GameServer.class);
                                Object repairObject = c.newInstance(testGameServer);

                                Method method = repairClass.getMethod("repair",new Class[]{long.class, String.class});
                                method.invoke(repairObject, new Object[]{1, "666"});
                            }catch (Exception e){
                                e.printStackTrace();
                                return false;
                            }
                            return true;
                        }
                    });
                    futureList.add(future);
                }
            }
        }

        boolean result = false;
        for(Future future : futureList){
            try {
                result = (boolean) future.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            if(!result){
                System.err.println("失败");
                break;
            }
        }

        DataMgr.outExcel(repairClassName);

        return true;
    }

    private void initMgr(){
        mgrList.add(new ServerMgr());
        mgrList.add(new ConfigMgr());
        mgrList.add(new GameMgr());
        mgrList.add(new ActivityMgr());
        mgrList.add(new ActivityScript());
        BaseMgr.setMgrList(mgrList);
        new RepairMainTaskMgr();
    }

    private <T> T getMgr(Class<T> T){
        for(BaseMgr baseMgr : mgrList){
            if(baseMgr.getClass() == T){
                return (T)baseMgr;
            }
        }
        return null;
    }
}
